package com.topscomm.gpm.script;

import com.topscomm.cbo.pojo.CboApprovalNodeInsEntity;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.SpringUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.gpm.GpmScript;
import com.topscomm.gpm.pojo.GpmProjectAppointmentEntity;
import com.topscomm.gpm.pojo.GpmProjectEntity;
import com.topscomm.gpm.service.impl.GpmProjectAppointmentService;
import com.topscomm.gpm.service.impl.GpmProjectService;
import com.topscomm.tap.ITapService;
import com.topscomm.tap.TapEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

public class GpmAppointmentSignerApprovalScript extends GpmScript {
    /**
     * 签发人审批后需要生成任命书编号
     *
     * @param service
     * @param entity
     * @throws Exception
     */
    @Transactional
    public void afterSignerApproval(ITapService service, TapEntity entity) throws Exception {
        // 审批节点实例
        CboApprovalNodeInsEntity nodeins = (CboApprovalNodeInsEntity) entity;
        String sourceid = ConvertUtil.convertToString(nodeins.getSourceid());
        GpmProjectAppointmentService gpmProjectAppointmentService = (GpmProjectAppointmentService) SpringUtil.getBean("gpmProjectAppointmentService");
        GpmProjectService gpmProjectService = (GpmProjectService) SpringUtil.getBean("gpmProjectService");
        // 获取任命书信息
        GpmProjectAppointmentEntity gpmProjectAppointmentEntity = gpmProjectAppointmentService.queryById(sourceid);
        if (gpmProjectAppointmentService.judgeSendTime(null, gpmProjectAppointmentEntity.getCreateon())) {
            return;
        }
        String projectCode = gpmProjectAppointmentEntity.getProjectcode();
        String sysPush = gpmProjectAppointmentEntity.getSyspush();
        String appointNumber = "RM" + gpmProjectAppointmentEntity.getDocno();
        gpmProjectAppointmentEntity.setAppointmentnumber(appointNumber);
        // 更新任命书编号信息
        gpmProjectAppointmentService.updateEntity(gpmProjectAppointmentEntity);
        List<GpmProjectEntity> sdProjectDoList = gpmProjectService.queryByWhere(" projectcode = '" + projectCode + "'");
        if (CollectionUtils.isEmpty(sdProjectDoList)) {
            return;
        }
        // 重新生成任命书
        gpmProjectAppointmentService.generateAppointment(gpmProjectAppointmentEntity, sdProjectDoList.get(0));
    }
}
